<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Ringo Chan <334258471@qq.com> <http://www.jxlnxx.com/>
// +----------------------------------------------------------------------

namespace Api\Controller;
use User\Api\UserApi;
use Discernbank\DiscernBank;
use Api\Model\MemberGiftModel;

/**
 * API用户控制器
 * 主要获取用户数据
 */
class UserController extends ApiController {

    /**
     * 用户登录
     * 
     * @author Ringo Chan<334258471@qq.com>
     */
	public function login(){
//	    $this->api_login() && response(-96);

	    params_exist(array('user', 'pwd', 'driver'));
	    
	    $username = I('post.user');
	    $password = I('post.pwd');
	    $driver = I('post.driver');
	   
		$user = new UserApi;

		$uid = $user->login($username, $password);

		if(0 < $uid){
			$Member = D('Member');

			if($Member->login($uid)){
			    M('member_token')->where(array('uid'=>$uid, 'deadline' => array('egt', NOW_TIME)))->setField('is_online',0);
				//发货后12天后自动修改为完成状态
				//$order = A('order')->autoTake($uid);
			    $token = addToken($uid, $driver);
				response(1, array('token'=>$token,'uid'=>$uid));
			} else {
				response(-1, '', $Member->getError());
			}

		} else {
			switch($uid) {
				case -1: $code = -7; $error = '用户不存在或被禁用！'; break;
				case -2: $code = -1; $error = '密码错误！'; break;
				default: $code = -9; $error = '未知错误！'; break;
			}			
			response($code, '', $error);
		}
	}
	
	/**
     * 用户注册
     * @author Ringo Chan<334258471@qq.com>
     */
	public function register(){
	    
	    params_exist(array('user', 'pwd', 'repwd', 'verify'));
	    
	    $username = I('post.user');
	    $password = I('post.pwd');
	    $repassword = I('post.repwd');
	    $verify = I('post.verify');
	    $referrer = I('post.referrer', 0);
	    
	    empty($verify) && response('-1', '', '验证码不能为空');
	    empty($username) && response('-1', '', '手机号码不能为空');
	    !preg_match('/1[34578]{1}\d{9}$/', $username) && response(-1, '', '请正确输入您的手机号码');
	    !preg_match('/^[a-zA-Z]\w{5,30}$/', $password) && response('-2', '', '密码首字符必须为英文字母，长度必须在6-30个字符之间');
	    $password != $repassword && response(-2, '', '两次密码输入不一致');
	    $Message_verify = M('Message_verify');
	    $map = array('type'=>'register', 'username'=>$username, 'code'=>$verify, 'deadline'=>array('egt', NOW_TIME), 'status' => 1);
	    $info = $Message_verify->where($map)->find();
	    $info || response('-4', '', '无效验证码或验证码已过期');
	    if (!empty($referrer)) {
	        $referrer2 = M('ucenter_member')->where(array('username'=>$referrer))->getField('id');
	        if(!$referrer2){
                response('-2', '', '推荐人手机号不存在');
			}
	    }
	    
        $User = new UserApi;
		$uid = $User->register($username, $password);
		if(0 < $uid){
		    $data1 = D('member')->create(array('nickname' => $username, 'referrer'=>$referrer, 'status' =>1,'uid'=>$uid));
            $avatar="http://ow7mta4mp.bkt.clouddn.com/default.jpg";
            $pic['url']=$avatar;
            $pic['status'] = 1;
            $pic['create_time']=time();
            $picid=M('picture')->add($pic);
            $data1['avatar']=$picid;
		    D('member')->add($data1);
		    //echo D('member')->getLastsql();
		    $Message_verify->where($map)->setField('status', 0);
		    //直播商城注册
            $user_pass=$this->setPass($password);
            $data=array(
            	'id' =>$uid,
                'user_login' => $username,
                'mobile' =>$username,
                'user_nicename' =>'手机用户'.substr($username,-4),
                'user_pass' =>$user_pass,
                'user_email' =>$uid,
                'signature' =>'这家伙很懒，什么都没留下',
                'avatar' =>$avatar,
                'avatar_thumb' =>$avatar,
                'last_login_ip' =>$_SERVER['REMOTE_ADDR'],
                'create_time' => date("Y-m-d H:i:s"),
                'last_login_time' => date("Y-m-d H:i:s"),
                'user_status' => 1,
                "user_type"=>2,//会员
                'free_viewing_time'=>2
            );
            $res=M('users','cmf_')->add($data);

            //赠送推荐人播豆
            //查询是否有推荐人
			if(!$referrer){
                $referrer1 = M('referrer')->where('mobile=' . $username)->getField('ref_mobile');
                if ($referrer1) {
                    M('member')->where('uid='.$uid)->setField('referrer',$referrer1);
                    $gift_coin = M('distribution', 'cmf_')->find();
                    $re = M('users', 'cmf_')->where('user_login=' . $referrer1)->setInc('coin', $gift_coin['gift_coin']);
                    $id=M('ucenter_member')->where('username='.$referrer1)->getField('id');
                    //邀请人获得播豆
                    $map['uid']=$id;
                    $map['order_id']=0;
                    $map['type']   = 9;
                    $map['money']  =$gift_coin['gift_coin'];
                    $map['month']  = date('Ym',time());;
                    $map['status'] = 1;
                    $map['createtime']=time();
                    $map['pur_type'] =2;
                    $re = M('user_trade')->add($map);
                }
            }
			response(1);
		} else {
			response($uid, '', $this->showRegError($uid));
		}
	}

    /**
     * 第三方登录
     */
	public function userLoginByThird(){
        params_exist(array('openid', 'type','nickname','avatar'));
		$data=I('');
		$openid =$data['openid'];
       /* $is_ios = strstr($openid,'oS-');
        if($is_ios){
			$openid=substr($openid, 3);
		}*/
		$avatar=I('avatar');
        $type=I('type');
        $nickname=I('nickname');
        $where['openid'] =$openid;
		$is_user=M('member')->where($where)->find();
		if(!$avatar){
			$avatar="http://ow7mta4mp.bkt.clouddn.com/default.jpg";
		}
        $User = new UserApi;
        $Member = D('Member');
		if(!$is_user){
            $username = $openid;
            $password = '123456';

            $uid = $User->register($username, $password);
            if (0 < $uid) {
                $pic['url']=$avatar;
                $pic['status'] = 1;
                $pic['create_time']=time();
                $picid=M('picture')->add($pic);

                $data1 = D('member')->create(array('nickname' => $nickname, 'status' => 1, 'uid' => $uid));
                $data1['login_type'] = $type;
                $data1['openid'] = $openid;
                $data1['avatar'] = $picid;
                D('member')->add($data1);

                //查询是否有推荐人
                $referrer1 = M('referrer')->where('mobile=' . $username)->getField('ref_mobile');
                if ($referrer1) {
                    M('member')->where('uid=' . $uid)->setField('referrer', $referrer1);
                    $gift_coin = M('distribution', 'cmf_')->find();
                    $re = M('users', 'cmf_')->where('user_login=' . $referrer1)->setInc('coin', $gift_coin['gift_coin']);
                    $id = M('ucenter_member')->where('username=' . $referrer1)->getField('id');
                    //邀请人获得播豆
                    $map['uid'] = $id;
                    $map['order_id'] = 0;
                    $map['type'] = 9;
                    $map['money'] = $gift_coin['gift_coin'];
                    $map['month'] = date('Ym', time());;
                    $map['status'] = 1;
                    $map['createtime'] = time();
                    $map['pur_type'] = 2;
                    $re = M('user_trade')->add($map);
                }
            } else {
                response($uid, '', $this->showRegError($uid));
            }
        }


        //登录
		$map['openid'] =$openid;
		$user=M('member')->where($map)->find();
           if($user){
               $data = array(
                   'login'           => array('exp', '`login`+1'),
                   'last_login_time' => NOW_TIME,
                   'last_login_ip'   => get_client_ip(1),
               );
               M('member')->where($map)->save($data);
             	$uid=$user['uid'];
             	//更新直播用户的 uid
			   $owhere['openid'] = $openid;
			   M('users','cmf_')->where($owhere)->setField('id',$uid);
               M('member_token')->where(array('uid'=>$uid))->delete();
               $u_map['username'] =$openid;
               $username =M('ucenter_member')->where($u_map)->getField('username');
               if($username == $openid){
               	$type=1; //没有绑定手机号
			}else{
               	$type=2; //已绑手机号
			}
            $token = addToken($uid, 'wx');
            response(1, array('token'=>$token,'type'=>$type,'uid'=>$uid));
           } else {
               response(-1, '', $Member->getError());
           }

	}

    /**
     * 绑定手机号
     */
	public function  bindMobile(){
        params_exist(array('mobile', 'verify','openid'));

        $username = I('post.mobile');
        $verify = I('post.verify');
		$openid=I('post.openid');
     /*   $is_ios = strstr($openid,'oS-');
        if($is_ios){
            $openid=substr($openid, 3);
        }*/
        empty($verify) && response('-1', '', '验证码不能为空');
        empty($username) && response('-1', '', '手机号码不能为空');

        $Message_verify = M('Message_verify');

        $map = array(
            'type'=>'forgotten',
            'username'=>$username,
            'code'=>$verify,
            'deadline'=>array('egt', NOW_TIME),
            'status' => 1
        );

        $info = $Message_verify->where($map)->find();

        !$info && response('-4', '', '无效验证码或验证码已过期');
        $user=M('ucenter_member')->where('username='.$username)->find();
        if($user){
            response(-9, '', '该手机号已注册或已绑定别的账号');
		}
        $where['openid']=$openid;
        $uid=M('member')->where($where)->getField('uid');
        $m_data['nickname']=$username;
       // M('member')->where($where)->save($m_data);
        $maps['username']=$username;
        $maps['update_time']=time();
        $res = M('ucenter_member')->where('id='.$uid)->save($maps);
        $data['user_login']=$username;
        $data['user_nicename']=$username;
        $c_maps['openid']=$openid;
        M('users','cmf_')->where($c_maps)->save($data);
		if($res){
            response(1);
        } else {
            response(-9, '', '绑定失败');
        }

	}

    /* 密码加密 */
    public function setPass($pass){
        $authcode='rCt52pF2cnnKNB3Hkp';
        $pass="###".md5(md5($authcode.$pass));
        return $pass;
    }

    /**
	 * 找回密码
	 *
	 * @author Ringo Chan<334258471@qq.com>
	 */
	public function forgotten(){
	     
	     params_exist(array('user', 'verify'));
	     
	     $username = I('post.user');
	     $verify = I('post.verify');
	      
	     empty($verify) && response('-1', '', '验证码不能为空');
	     empty($username) && response('-1', '', '手机号码不能为空');
	      
	     $Message_verify = M('Message_verify');
	      
	     $map = array(
	         'type'=>'forgotten',
	         'username'=>$username,
	         'code'=>$verify,
	         'deadline'=>array('egt', NOW_TIME),
	         'status' => 1
	     );
	      
	     $info = $Message_verify->where($map)->find();
	     
	     !$info && response('-4', '', '无效验证码或验证码已过期');
	     
	     $verify = getRandCode(16, 'string');
	     
	     $data = array(
	         'username' => $username,
	         'verify' => $verify,
	         'create_time' => NOW_TIME,
	         'deadline' => NOW_TIME + 1800,
	         'status' => 1
	     );
	     
	     M('Member_forgotten')->add($data) && $Message_verify->where($map)->setField('status', 0);
	     
	     response(1, array('verify'=>$verify));
	}
	
	/**
	 * 找回密码->设置密码
	 * 
	 * @author Ringo Chan<334258471@qq.com>
	 */
	public function resetPassword(){
//	    $this->api_login() && response(-96);
	    
	    params_exist(array('user', 'verify', 'pwd', 'repwd'));
	    
	    $username = I('post.user');
	    $verify = I('post.verify');
	    $password = I('post.pwd');
	    $repassword = I('post.repwd');
	    
	    empty($verify) && response('-1', '', '验证码不能为空');
	    empty($username) && response('-1', '', '手机号码不能为空');
	    !preg_match('/^[a-zA-Z]\w{6,30}$/', $password) && response('-2', '', '密码首字符必须为英文字母，长度必须在6-30个字符之间');
	    $password != $repassword && response(-2, '', '两次密码输入不一致');
	    
	    $map = array(
	        'username'=>$username, 
	        'code'=>$verify, 
	        'deadline'=>array('egt', NOW_TIME),
	        'status'=>1
	    );
	    
	    $info = M('Message_verify')->where($map)->find();
	    // echo M('Message_verify')->getLastsql();
	    // dump($info);
	    !$info && response(-4, '', '验证失效');
	    
	    $user = new UserApi();
	    
	    $res = $user->updatePassword($username, $password);
	    
	    if ($res['status']){
            //修改直播密码
            $user_pass=$this->setPass($password);
            $data['user_pass']=$user_pass;
            $res=M('users','cmf_')->where('user_login='.$username)->save($data);
	        M('Message_verify')->where($map)->setField('status', 0);
	        response(1);
	    } else {
	        response(-9, '', $res['info']);
	    }
	}
	
	/**
	 * 获取用户昵称
	 * 
	 * @param string token 登录标识
	 * @author Ringo Chan<334258471@qq.com>
	 */
	public function getNickname(){
	    !api_login() && response(-94);
	    !passed_login() && response(-95);
	    !only_login() && response(-96);

	    $this->getMemberInfo('nickname');
	}
	
	/**
	 * 编辑用户昵称
	 * 
	 * @param string token 登录标识
	 * @param string nickname
	 * @author Ringo Chan<334258471@qq.com>
	 */
	public function editNickname(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
	    
	    params_exist(array('nickname'));
	    
	    $nickname = I('post.nickname');
	    
	    empty($nickname) && response('-1', '', '昵称不能为空');
	   // !prg_match('/^[\x{4e00}-\x{9fa5}A-Za-z][\x{4e00}-\x{9fa5}A-Za-z0-9_]{3,30}+$/u', $nickname) && response(-1, '', '昵称格式错误，长度必须在1~10个字符或首字母不能为数字下划线');
		if(strlen($nickname)>30){
            response(-1, '', '昵称格式错误，长度必须在1~10个字符');
		}
		$uid=API_UID;
		$phone=M('ucenter_member')->where('id='.$uid)->getField('username');
		$data['user_nicename'] = $nickname;
        $data['last_login_time'] = date('Y-m-d H:i:s', time());;
		$re = M('users','cmf_')->where('user_login='.$phone)->save($data);
	    $Member = D('Member');

	    $Member->where(array('uid'=>API_UID))->setField('nickname', $nickname) ? response(1) : response(-9, '', '未知错误');
	}
	
	/**
	 * 修改用户密码
	 * 
	 * @param string token 登录标识
	 * @param string oldpwd 旧密码
	 * @param string newpwd 新密码
	 * @param string repwd 确认密码
	 * @author Ringo Chan<334258471@qq.com>
	 */
	public function editPassword(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
	    
	    params_exist(array('oldpwd', 'newpwd', 'repwd'));
	    
	    $oldpwd = I('post.oldpwd');
	    $newpwd = I('post.newpwd');
	    $repwd = I('post.repwd');
	    
	    empty($oldpwd) && response(-1, '', '旧密码不能为空');
	    empty($newpwd) && response(-1, '', '新密码不能为空');
	    !preg_match('/^[a-zA-Z]\w{6,30}$/', $newpwd) && response('-2', '', '密码首字符必须为英文字母，长度必须在6-30个字符之间');
	    $newpwd != $repwd && response(-1, '', '两次密码输入不一致');
	    
	    $user = new UserApi();
	    !$user->verifyUser(API_UID, $oldpwd) && response(-2, '', '旧密码不匹配，请重新输入');
	    
	    $Ucenter_member = M('Ucenter_member');
	    $info = $Ucenter_member->find(API_UID);
	     
	    $user = new UserApi();
	     
	    $res = $user->updatePassword($info['username'], $newpwd);
		//修改直播密码
        $user_pass=$this->setPass($newpwd);
        $data['user_pass']=$user_pass;
        $res=M('users','cmf_')->where('user_login='.$info['username'])->save($data);
	    if ($res){
	        response(1);
	    } else {
	        response(-9, '', $res['info']);
	    }
	}
	
	/**
	 * 获取用户性别
	 * 
	 * @param string token 登录标识
	 * @author Ringo Chan<334258471@qq.com>
	 */
	public function getSex(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);

	    $this->getMemberInfo('sex');
	}
	
	/**
	 * 修改用户性别
	 * 
	 * @param string token 登录标识
	 * @param integer sex
	 * @author Ringo Chan<334258471@qq.com>
	 */
	public function editSex(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
	     
	    params_exist(array('sex'));
	    
	    $sex = I('post.sex');
	    
	    !in_array($sex, array(0, 1, 2)) && response(-1, '', '错误性别');

        $uid=API_UID;
        $phone=M('ucenter_member')->where('id='.$uid)->getField('username');
        $data['sex'] = $sex;
        $data['last_login_time'] = date('Y-m-d H:i:s', time());;
        $re = M('users','cmf_')->where('user_login='.$phone)->save($data);

	    $Member = D('Member');
        $data['sex'] =$sex;
        $data['last_login_time']=time();
        $flag=$Member->where(array('uid'=>API_UID))->save($data);
        $flag? response(1) : response(-9, '', '未知错误');
	}
	
	/**
	 * 获取用户手机号码
	 *
	 * @param string token 登录标识
	 * @author Ringo Chan<334258471@qq.com>
	 */
	public function getMobile(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
	
	    $Ucenter_member = M('Ucenter_member');
	    $info = $Ucenter_member->find(API_UID);
	    
	    $info ? response(1, array('mobile'=>$info['mobile'])) : response(-9);
	}
	
	/**
	 * 修改用户手机号码
	 *
	 * @param string token 登录标识
	 * @param string mobile 更改的手机号
	 * @param string verify 验证码
	 * @author Ringo Chan<334258471@qq.com>
	 */
	public function editMobile(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
	
	    params_exist(array('mobile', 'verify'));
	    
	    $verify = I('post.verify');
	    $mobile = I('post.mobile');
	    
	    empty($verify) && response('-1', '', '验证码不能为空');
	     
	    $Message_verify = M('Message_verify');
	    
	    $map = array(
	        'type'=>'modifymobile',
	        'username'=>$mobile,
	        'code'=>$verify,
	        'deadline'=>array('egt', NOW_TIME),
	        'status' => 1
	    );
	     
	    $info = $Message_verify->where($map)->find();
	    
	    !$info && response('-4', '', '无效验证码或验证码已过期');
	     
	    !preg_match('/1[3458]{1}\d{9}$/', $mobile) && response(-1, '', '请正确输入您的手机号码');
	    get_table_field($mobile, 'username', 'id', 'ucenter_member') && response(-2, '', '手机号码已注册');
	    
	    $Message_verify->where($map)->setField('status', 0);
	     
	    $Ucenter_member = M('Ucenter_member');
	     
	    $Ucenter_member->where(array('id'=>API_UID))->save(array('mobile'=>$mobile, 'username'=>$mobile)) ? response(1) : response(-9);
	}
	
	/**
	 * 获取收货地址列表
	 * 
	 * @param string token 登录标识
	 * @author Ringo Chan<334258471@qq.com>
	 */
	public function getReceiveAddrs(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
	    
	    $list = array();
	    
	    $Member_receive = M('Member_receive');
	    
	    $results = $Member_receive->where(array('uid'=>API_UID))->order('update_time DESC')->select();
	    
	    foreach ($results as $result) {
	        $list[] = array(
	            'id' => $result['id'],
	            'realname' => $result['realname'],
	            'mobile' => $result['mobile'],
	            'address' => $result['address'],
	            'province_id' => $result['province_id'],
	            'province' => get_table_field($result['province_id'], 'province_id', 'province', 'hat_province'),
	            'city_id' => $result['city_id'],
	            'city' => get_table_field($result['city_id'], 'city_id', 'city', 'hat_city'),
	            'area_id' => $result['area_id'],
	            'area' => get_table_field($result['area_id'], 'area_id', 'area', 'hat_area'),
	            'is_default' => $result['is_default']
	        );
	    }
	    
	    response(1, $list);
	}
	
	/**
	 * 获取用户收货地址详情
	 * 
	 * @param string token 登录标识
	 * @param integer id 收货地址编号
	 * @author Ringo Chan<334258471@qq.com>
	 */
	public function getReceiveAddr(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
	    
	    params_exist(array('id'));
	    
	    $id = I('post.id');
	    
	    $Member_receive = M('Member_receive');
	    
	    $info = $Member_receive->where(array('id'=>$id, 'uid'=>API_UID))->field(array('mobile', 'realname', 'province_id', 'city_id', 'area_id', 'is_default', 'address'))->find();
	    $info['province'] = get_table_field($info['province_id'], 'province_id', 'province', 'hat_province');
	    $info['city'] = get_table_field($info['city_id'], 'province_id', 'city', 'hat_city');
	    $info['area'] = get_table_field($info['area_id'], 'province_id', 'area', 'hat_area');
	    
	    $info ? response(1, $info) : response(-1, '', '没有找到匹配数据');
	}
	
	/**
	 * 获取删除地址
	 *
	 * @param string token 登录标识
	 * @param integer id 收货地址编号
	 */
	public function delReceiveAddr(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
	     
	    params_exist(array('id'));
	     
	    $id = I('post.id');
	     
	    $Member_receive = M('Member_receive');
	     
	    $map=array('id'=>$id, 'uid'=>API_UID);
	    $info = $Member_receive->where($map)->find();
	     
	    empty($info) && response(-1, '', '地址不存在');
	    
	    $Member_receive->where($map)->delete() ? response(1) : response(-9);
	}
	
	/**
	 * 添加用户收货地址
	 * 
	 * @param string token 登录标识
	 * @param string mobile 联系手机号码
	 * @param string realname 姓名
	 * @param integer province_id 省份ID
	 * @param integer city_id 城市ID
	 * @param integer area_id 县\区ID
	 * @param string address 详细地址
	 * @author Ringo Chan<334258471@qq.com>
	 */
	public function addReceiveAddr(){

        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
	    
	    params_exist(array('mobile', 'realname', 'province_id', 'city_id', 'area_id', 'address'));
	    
	    $mobile = I('post.mobile');
	    $realname = I('post.realname');
	    $province_id = I('post.province_id', 0);
	    $city_id = I('post.city_id', 0);
	    $area_id = I('post.area_id', 0);
	    $address = I('post.address');
	    $is_default = I('post.is_default',0);
	    
	    !preg_match('/1[3458]{1}\d{9}$/', $mobile) && response(-1, '', '请正确输入您的手机号码');
	    !preg_match('/^0|1$/', $is_default) && response(-1, '', '是否为默认地址只能为0或1');
	    !preg_match('/^[\x{4e00}-\x{9fa5}]{1,10}+$/u', $realname) && response(-1, '', '请输入联系人，必须为中文');
	    !get_table_field($province_id, 'province_id', 'province_id', 'Hat_province') && response(-1, '', '选择的省份没有匹配对象');
	    !get_table_field($city_id, 'city_id', 'city_id', 'Hat_city') && response(-1, '', '选择的城市没有匹配对象');
	    !get_table_field($area_id, 'area_id', 'area_id', 'Hat_area') && response(-1, '', '选择的区县没有匹配对象');
	    !preg_match('/^[\x{4e00}-\x{9fa5}][\x{4e00}-\x{9fa5}A-Za-z0-9_-]{5,50}+$/u', $address) && response(-1, '', '详细地址长度必须在5~50个字符');
	    	        
	    
	    $data = array(
	        'uid' => API_UID,
	        'realname' => $realname,
	        'mobile' => $mobile,
	        'province_id' => $province_id,
	        'city_id' => $city_id,
	        'area_id' => $area_id,
	        'address' => $address,
	        'is_default' => $is_default,
	        'create_time' => NOW_TIME,
	        'update_time' => NOW_TIME
	    );
	    
	    $Member_receive = M('Member_receive');
	    if($is_default){
	        $Member_receive->where(array('uid'=>API_UID))->setfield('is_default',0);
	    }
	    
	    $Member_receive->add($data) ? response(1) : response(-9);
	}
	
	/**
	 * 编辑用户收货地址
	 *
	 * @param integer id 收货地址编号
	 * @param string token 登录标识
	 * @param string mobile 联系手机号码
	 * @param string realname 姓名
	 * @param integer province_id 省份ID
	 * @param integer city_id 城市ID
	 * @param integer area_id 县\区ID
	 * @param string address 详细地址
	 * @author Ringo Chan<334258471@qq.com>
	 */
	public function editReceiveAddr(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
	     
	    params_exist(array('id', 'mobile', 'realname', 'province_id', 'city_id', 'area_id', 'address'));
	    
	    $id = I('post.id');
	    $mobile = I('post.mobile');
	    $realname = I('post.realname');
	    $province_id = I('post.province_id', 0);
	    $city_id = I('post.city_id', 0);
	    $area_id = I('post.area_id', 0);
	    $address = I('post.address');
	    $is_default = I('post.is_default',0);
	    
	    $Member_receive = M('Member_receive');
	   
	    !$Member_receive->where(array('id'=>$id, 'uid'=>API_UID))->find() && response(-1, '', '参数ID错误');
	    
	    !preg_match('/^0|1$/', $is_default) && response(-1, '', '是否为默认地址只能为0或1');
	    !preg_match('/1[3458]{1}\d{9}$/', $mobile) && response(-1, '', '请正确输入您的手机号码');
	    !preg_match('/^[\x{4e00}-\x{9fa5}]{1,10}+$/u', $realname) && response(-1, '', '请输入联系人，必须为中文');
	    !get_table_field($province_id, 'province_id', 'province_id', 'Hat_province') && response(-1, '', '选择的省份没有匹配对象');
	    !get_table_field($city_id, 'city_id', 'city_id', 'Hat_city') && response(-1, '', '选择的城市没有匹配对象');
	    !get_table_field($area_id, 'area_id', 'area_id', 'Hat_area') && response(-1, '', '选择的区县没有匹配对象');
	    !preg_match('/^[\x{4e00}-\x{9fa5}][\x{4e00}-\x{9fa5}A-Za-z0-9_-]{1,50}+$/u', $address) && response(-1, '', '详细地址长度必须在5~50个字符');
	    //!preg_match('/^.{5,50}+$/', $address) && response(-1, '', '详细地址长度必须在5~50个字符');
	     
	    $data = array(
	        'id' => $id,
	        'realname' => $realname,
	        'mobile' => $mobile,
	        'province_id' => $province_id,
	        'city_id' => $city_id,
	        'area_id' => $area_id,
	        'address' => $address,
	        'is_default' => $is_default,
	        'update_time' => NOW_TIME
	    );	     
	    if($is_default){
	        $Member_receive->where(array('uid'=>API_UID))->setfield('is_default',0);
	    }    
	    	    
	    $Member_receive->save($data) ? response(1) : response(-9);
	}
	
	/**
	 * 获取用户注册错误信息
	 * 
	 * @param  integer $code 错误编码
	 * @return string        错误信息
	 */
	private function showRegError($code = 0){
	    switch ($code) {
	        case -1:  $error = '用户名长度必须在16个字符以内！'; break;
	        case -2:  $error = '用户名被禁止注册！'; break;
	        case -3:  $error = '用户名被占用！'; break;
	        case -4:  $error = '密码长度必须在6-30个字符之间！'; break;
	        case -5:  $error = '邮箱格式不正确！'; break;
	        case -6:  $error = '邮箱长度必须在1-32个字符之间！'; break;
	        case -7:  $error = '邮箱被禁止注册！'; break;
	        case -8:  $error = '邮箱被占用！'; break;
	        case -9:  $error = '手机格式不正确！'; break;
	        case -10: $error = '手机被禁止注册！'; break;
	        case -11: $error = '手机号被占用！'; break;
	        default:  $error = '未知错误';
	    }
	    return $error;
	}
	
	/**
	 * 获取用户字段值
	 * 
	 * @param string $field 字段属性
	 * @return string 字段值
	 * @author Ringo Chan<334258471@qq.com>
	 */
	private function getMemberInfo($field){
	    $Member = D('Member');
	     
	    $info = $Member->where(array('uid'=>API_UID))->find();
	     
	    $info ? response(1, array('nickname'=>$info[$field])) : response(-9, '', '未知错误');
	}
	/**
	 * 更改头像
	 * @param int cover_id
	 */
	public function updateAvatar(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
	    params_exist(array('cover_id'));
	    
	    $M_mbmber=M('member');
	    $cover_id=I('cover_id');
	    $M_mbmber->where(array('uid'=>API_UID))->setfield('avatar',$cover_id)  ?
	    response(1) : response(-9);
	    
	}
	/**
	 * 添加银行卡
	 * @param string card_no
	 * @param string true_name
	 * @param int isdefault
	 */
	public function addBankCard(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
	    params_exist(array('card_no','true_name'));
	       
	    $M_bank_card=M('bank_card');
	    
	    $data = params_post(array('card_no','true_name','is_default'));
	   
	    $is_default=I('post.is_default',0);
	    $data['is_default']=$is_default;
	    ! preg_match('/^0|1$/', $is_default) && response(- 1,'', '是否为默认值只能是0或1');
	    ! preg_match('/^.{0,50}$/', $data['true_name']) && response(- 1,'','真实姓名在20字以内');
	    ! preg_match('/^.{16,19}$/', $data['card_no']) && response(- 1,'', '银行卡号格式错误');
	    
	    $M_bank_card->where(array('card_no'=>$data['card_no'],
	        'uid'=>API_UID))->find() && response(-1,'','已经添加当前银行卡');
	    $data['uid']=API_UID;
	    $data['create_time']=NOW_TIME;
	    if($data['is_default']){
	      $M_bank_card->where(array('uid'=>API_UID))->setfield('is_default',0);
	    }
	    
	    $discrenbank = new DiscernBank();
	    $data['bank_title']=$discrenbank->discern($data['card_no']);
	    if(!$M_bank_card->where(array('uid'=>API_UID,'is_default'=>1))->find()){	    
	        $data['is_default']=1;
	    }
	    $M_bank_card->add($data) ? response(1) : response(9);	    
	}
	
	/**
	 * 银行卡列表
	 */
	public function bankcardList(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
	    $M_bank_card=M('bank_card');
	    $map=array('uid'=>API_UID);
	    $list=$M_bank_card->where($map)->field('id,card_no,true_name,is_default,bank_title')->select();
	    $total=$M_bank_card->where($map)->count();
	    
	    $result=array();
	    $result['total']=$total;
	    $result['result']=$list;
	    
	    $list ? response(1,$result) : response(-2);
	}
	/**
	 * 删除银行卡
	 * @param int id
	 */
	public function delBankCard(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
	    params_exist(array('id'));
	    
	    $id=I('post.id');
	    !is_numeric($id) && response(-1,'','id错误');
	    $map=array('id'=>$id,'uid'=>API_UID);
	    
	    $M_bank_card=M('bank_card');
	    !$M_bank_card->where($map)->find() && response(-2,'','银行卡不存在');
	    
	    $result=$M_bank_card->where($map)->delete();
	    if($result){
	        $map['is_default']=1;
	        if(!$M_bank_card->where($map)->find()){	               
	               $otm_id=$M_bank_card->where(array('uid'=>API_UID))->getfield('id');
	                $M_bank_card->where(array('id'=>$otm_id))->setfield('is_default',1);
	            }
	        }
	    
	    $result ? response(1) : response(-9);	    
	}
	
	/**
	 * 设置默认银行卡
	 * @param int id
	 */
	public function setdefaultCard(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
	    params_exist(array('id'));
	     
	    $id=I('post.id');
	    !is_numeric($id) && response(-1,'','id错误');
	    $map=array('id'=>$id,'uid'=>API_UID);
	    
	    $M_bank_card=M('bank_card');
	    !$M_bank_card->where($map)->find() && response(-2,'','银行卡不存在');
	     
	    $M_bank_card->where(array('uid'=>API_UID))->setfield('is_default',0);
	    
	    $M_bank_card->where($map)->setfield('is_default',1) !==false  ? response(1) : response(-9);
	}
	
	/**
	 * 统计
	 */
	public function statistics(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
	     
	    $result=array();
	    $map=array();
	    $map['uid']=API_UID;
	     
	    $result['collect_product']=M('collect_product')->where($map)->count();
	    $result['collect_shop']=M('collect_shop')->where($map)->count();
	    $result['collect_browse']=M('collect_browse')->where($map)->count();
	     
	    $result['order_no_pay']=M('order')->where(array('uid'=>API_UID,'status'=>0))->count();
	    $result['order_no_shipments']=M('order')->where(array('uid'=>API_UID,'status'=>1))->count();
	    $result['order_no_receipt ']=M('order')->where(array('uid'=>API_UID,'status'=>array('BETWEEN','2,4')))->count();
	    $result['order_no_comment']=M('order')->where(array('uid'=>API_UID,'status'=>5))->count();
	    $result['order_goods_return']=M('order')->where(array('uid'=>API_UID,'status'=>6))->count();
	     
	    response(1,$result);
	}
	/**
	 * 我的钱包
	 */
	public function Mywallet(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
	    
	    $result=array();
	    $map=array();
	    $map['uid']=API_UID;
	    
	    $wallet=M('member')->where(array('uid'=>API_UID))->field('uid,amount')->find();
	    
	    response(1,$wallet);
	}

    /**
     * 消费记录
     */
	public function myRecord(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);

        $uid= API_UID;
        $page = I('post.page', 1);
        $page_size = I('post.page_size', 10);
        $offset = ($page - 1) * $page_size;
        $map['status'] =1;
        $map['uid'] =$uid;
		$list = M('user_trade')->where($map)->limit($offset, $page_size)->order('createtime desc')->select();
		foreach ($list as &$li){
			if($li['type'] == '1'){
				$li['type_txt'] = "充值";
			}else if ($li['type'] == '2'){
				if($li['paytype'] ==1){
                    $li['type_txt'] = "申请提现";
				}else{
                    $li['type_txt'] = "提现成功";
                }
			}else if ($li['type'] == '3'){
                $li['type_txt'] = "购买";
            }else if ($li['type'] == '4'){
                $li['type_txt'] = "卖出商品";
            }else if ($li['type'] == '5'){
                $li['type_txt'] = "分销佣金";
            }else if ($li['type'] == '6'){
                $li['type_txt'] = "退款";
            }else if($li['type'] ==7){
            	$li['type_txt'] = "购买商品赠送";
			}else if($li['type'] == 8){
                $li['type_txt'] = "播豆转账";
			}else if($li['type'] == 9){
				$li['type_txt'] = "邀请送播豆";
			}else if($li['type'] == 10){
                $li['type_txt'] = "卖商品减播豆";
			}else if($li['type'] == 11){
                $li['type_txt'] = "购买视频";
			}else if($li['type'] == 12){
                $li['type_txt'] = "下载收费";
            }else if($li['type'] == 13){
                $li['type_txt'] = "打赏";
            }else if($li['type'] == 14){
                $li['type_txt'] = "视频收益";
            }else if($li['type'] == 15){
                $li['type_txt'] = "推荐收费";
            }

            $li['month'] = date('m月d日',$li['createtime']);
            if($li['pur_type'] ==2){
                $li['money']=$li['money']."播豆";
			}else{
                $li['money']=$li['money']."元";
			}
		}

        $list && response(1,$list) && response(-2);
	}
	
	
	/**
	 * 用户头像
	 */
    public function avatarPath(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $M_member=M('member');
        $user_info=$M_member->where(array('uid'=>API_UID))->find();
        $avatar=M('picture')->where('id='.$user_info['avatar'])->getField('url');
        if(!$avatar){
        	$avatar='';
		}
        $user_info ? response(1,array('avatar_path'=>$avatar)) : resourcebundle_count(-2);
    }
    
    /**
     * 修改头像
     * @param int cover_id
     */
    public function modifyAvatar(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        params_exist(array('cover_id'));
        $uid=API_UID;
        $cover_id=I('post.cover_id');
        !get_table_field($cover_id,'id','id','picture') && response(-2,'上传的头像不存在');
        $M_member=M('member');
        $url=M('picture')->where('id='.$cover_id)->getField('url');
        //修改直播的头像
		$phone =M('ucenter_member')->where('id='.$uid)->getField('username');
		$data['avatar']=$url;
        $data['avatar_thumb']=$url;
		$re=M('users','cmf_')->where('user_login='.$phone)->save($data);
        $M_member->where(array('uid'=>API_UID))->setfield('avatar', $cover_id) !==false ? response(1) : response(-9);
        
    }
    
    /*
     * 获取用户基本信息
     */
    public function BaseInfo()
    {
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
    
        $Member = M('Member');
    	//var_dump(I());
        $info = $Member->where(array(
            'uid' => API_UID
        ))
        ->field('uid',
                'nickname',
                'sex',
                'qq',
                'money',
                'avatar',
                'birthday',
                'score',
                'level',
                'last_login_ip',
                'last_login_time',
                'freeze_amount',
				'openid',
                'amount') ->find();
    //  echo  $Member->getLastsql();
        if ($info) {
            //更新直播用户的 uid
            $owhere['openid'] = $info['openid'];
            M('users','cmf_')->where($owhere)->setField('id',$info['uid']);
            //$info['avatar_path'] = get_url($info['avatar']);
            $info['avatar_path'] =  M('Picture')->where('id='.$info['avatar'])->getField('url');
        }
       
        $ext_info=M('ucenter_member')->where(  array( 'id' => API_UID )) ->field( array( 'username', 'email')) ->find();
        if ($info  && $ext_info) {
            $info=array_merge($info,$ext_info);
        } 
        
        $info && response(1,$info) && response(-2);
    }

	/**
	 * 赠送转让播豆
	 */
	public function giveScore(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $uid=API_UID;
		//--uid 赠送给谁那个用户 id score_num 赠送数量
		params_exist(array('uid','score_num'));
		//--赠送数量
		$score_num = (int)I('post.score_num');
		if($score_num<1){
			response(-1, '赠送数量不能小于1');
		}
		//--赠送给谁
		$username = I('post.uid');
		$mymobile=M('ucenter_member')->where('id='.$uid)->getField('username');
		$coin =M('users','cmf_')->where('user_login='.$mymobile)->getField('coin');
		if($coin>$score_num){
			$member =M('users','cmf_')->where('user_login='.$username)->find();
			if($member){
				//扣除手续费
				$ratio = M('config_private','cmf_')->getField('transfer_ratio');
				$score =$score_num-$score_num*$ratio/100;
				//增加对方播豆
				M('users','cmf_')->where('user_login='.$username)->setInc('coin',$score);
				$zuid =M('ucenter_member')->where('username='.$username)->getField('id');
                //收款方播豆转账记录
                payrecord($zuid, 0,$score,8, 2);
				//扣除自己播豆
                M('users','cmf_')->where('user_login='.$mymobile)->setDec('coin',$score_num);
                //转账方播豆转账记录
                payrecord($uid, 0,$score_num,8, 2);

				response(1,'播豆转账成功');
			}else{
                response(-1,'此用户不存在');
			}
		}else{
            response(-1,'您的播豆不足！');
		}
		exit;

		$score = M('member')->where('uid='.API_UID)->getField('score');
		if($score>I('post.score_num')){
			$member = M('ucenter_member')->Where('username='.$username)->join('__MEMBER__ m on __UCENTER_MEMBER__.id=m.uid')->find();
			IF($member['uid']==API_UID){
				response(-1,'您不能自己赠送自己积分');
			}
			if($member){

                if(M('member')->where('uid='.$member['id'])->Save(array(
                    'score' => $member['score']+$score_num
                ))){
                    M('member')->Where('uid='.API_UID)->Save(array(
                        'score' => $score-$score_num,
                    ));
                    response(1,'积分赠送成功');
                }
                exit;
				$shop_u=M('shop')->where(array('uid'=>$member['uid']))->find();
                $shop_u_s=M('shop')->where(array('uid'=>API_UID))->find();
                if(empty($shop_u) && empty($shop_u_s)){
                    if(M('member')->where('uid='.$member['id'])->Save(array(
                        'score' => $member['score']+$score_num
                    ))){
                        M('member')->Where('uid='.API_UID)->Save(array(
                            'score' => $score-$score_num,
                        ));
                        response(1,'积分赠送成功');
                    }

                }else if(!empty($shop_u) && empty($shop_u_s)){
                    M('shop')->where(array('uid'=>$member['uid']))->setInc('recover_score',$score_num);
                    M('member')->Where('uid='.API_UID)->Save(array(
                        'score' => $score-$score_num,
                    ));
                    response(1,'积分赠送成功');
                }else if(!empty($shop_u) && !empty($shop_u_s)){

                    M('shop')->where(array('uid'=>$member['uid']))->setInc('recover_score',$score_num);
                    M('shop')->where(array('uid'=>API_UID))->setInc('give_score',$score_num);
                    M('member')->Where('uid='.API_UID)->Save(array(
                        'score' => $score-$score_num,
                    ));
                    response(1,'积分赠送成功');
                }else if(empty($shop_u) && !empty($shop_u_s)){
                    if(M('member')->where('uid='.$member['id'])->Save(array(
                        'score' => $member['score']+$score_num
                    ))){
                    	M('shop')->where(array('uid'=>API_UID))->setInc('give_score',$score_num);
                        M('member')->Where('uid='.API_UID)->Save(array(
                            'score' => $score-$score_num,
                        ));
                        response(1,'积分赠送成功');
                    }
                }else{
                    response(-1,'未知错误');
                }


			}else{
				response(-1,'此用户不存在');
			}
		}else{
			response(-1,'您的积分不足！');
		}
	}

	/**
	 * 获取用户积分
	 */
    public function getScore(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);

	    $Ucenter_member = M('member');
	    $info = $Ucenter_member->find(API_UID);
	    $info ? response(1, array('score'=>$info['score']),'积分获取成功') : response(-9);
    }
    public function sign() {
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $filters['create_time'][] = array('egt', strtotime(date('Y-m-d 00:00:00')));
        $filters['create_time'][] = array('elt', strtotime(date('Y-m-d 23:59:59')));
        $filters['uid'] = array('eq', API_UID);
        $info = M('member_sign')->where($filters)->find();
        $info && response(-1, null, '已签到');
        $id = M('member_sign')->add(array('uid'=>API_UID, 'create_time'=>NOW_TIME));
        $id > 0 || response(-9);
        M('member_gift')->add(array('uid' => API_UID, 'gift' => MemberGiftModel::ZHANGSHENG, 'create_time' => NOW_TIME));
        $userInfo = M('member')->where(array('uid'=>API_UID))->find();
        $total = M('member_sign')->where(array('uid'=>API_UID))->count() | 0;
        $level = calculation_level($total);
        if ($userInfo['level'] < $level) {
            M('member')->save(array('uid'=>API_UID, 'level'=>$level));
            for ($i = $userInfo['level']; $i < $level; $i++) {
                for ($j = 0; $j <= $i; $j++) {
                    M('member_gift')->add(array('uid' => API_UID, 'gift' => MemberGiftModel::XIAOMUZHI, 'create_time' => NOW_TIME));
                }
            }
        }
        response(1);
    }
    
    public function gift() {
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $result = array();
        $result['zhangsheng']['total'] = M('member_gift')->where(array('uid'=>API_UID, 'gift'=>MemberGiftModel::ZHANGSHENG))->count() | 0;
        $result['zhangsheng']['unused'] = M('member_gift')->where(array('uid'=>API_UID, 'gift'=>MemberGiftModel::ZHANGSHENG, 'is_used'=>0))->count() | 0;
        $result['xiaomuzhi']['total'] = M('member_gift')->where(array('uid'=>API_UID, 'gift'=>MemberGiftModel::XIAOMUZHI))->count() | 0;
        $result['xiaomuzhi']['unused'] = M('member_gift')->where(array('uid'=>API_UID, 'gift'=>MemberGiftModel::XIAOMUZHI, 'is_used'=>0))->count() | 0;
        
        response(1, $result);
    }
    
    public function useGift() {
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $shopId = I('post.shopId', 0);
        $gift = I('post.gift', 0);
        $quantity = I('post.quantity', 1);
        $giftInfo = getGifts($gift);
        (empty($giftInfo) || is_array($giftInfo)) && response(-1, null, '参数有误');
        $shopInfo = M('shop')->where(array('id'=>$shopId, 'apply_uid'=>array('gt',0), 'status'=>1))->find();
        $shopInfo || response(-1, null, '指定店铺不存在');
        intval($quantity) < 1 && response(-1, null, '使用数量至少为1');
        $total = M('member_gift')->where(array('uid'=>API_UID, 'gift'=>$gift, 'is_used'=>0))->count() | 0;
        ($total > 0 && $total >= intval($quantity)) || response(-2, null, '数量不足');
        if ($gift == MemberGiftModel::ZHANGSHENG) {
            $field = 'gift_zhangsheng';
        } elseif ($gift == MemberGiftModel::XIAOMUZHI) {
            $field = 'gift_xiaomuzhi';
        } else {
	        response(-1,'礼物类型有误');
        }
        $memberGifts = M('member_gift')->where(array('uid'=>API_UID, 'gift'=>$gift, 'is_used'=>0))->limit(0,$quantity)->select();
        foreach ($memberGifts as $memberGift) {
            M('member_gift')->where(array('id'=>$memberGift['id']))->save(array('is_used'=>1, 'use_time'=>NOW_TIME, 'sid'=>$shopId));
        }
        M('shop')->where(array('id'=>$shopId))->setInc($field, $quantity);
        response(1);
    }

    /**
	 * 申请提现
     * @return mixed
     */
    public  function  cash(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $uid=API_UID;
        $pay_account = I('post.pay_account', 0);
        $money = I('post.money', 0);
        if(!$pay_account){
            response(-1,'','请输入支付宝账号');
        }
        $amount = M('member')->where('uid='.$uid)->getField('amount');
        if ($amount<$money){
            response(-1,'','请填写合理的提现的金额');
        }
        $is_shop=M('shop')->where('uid='.$uid)->find();
        if(!$is_shop){
			response(-1,'','开通商城里面的我是商家功能才能提现');
		}
		$phone=M('ucenter_member')->where('id='.$uid)->getField('username');
        $z_uid=M('users','cmf_')->where('user_login='.$phone)->getField('id');
        $liverec=M('users_liverecord ','cmf_')->where('uid='.$z_uid)->select();
        $mins =0;
        foreach ($liverec as $rec){

			$res = timeDiff($rec['starttime'],$rec['endtime']);
			$mins += $res['min'];
		}
		if($mins <=10){
            response(-1,'','直播时间必须大于10分钟才可以提现');
		}

        $res1 =  M('member')->where('uid='.$uid)->setDec('amount',$money);
        $data = [];
        $data['uid'] = $uid;
        $data['type'] = 2;
        $data['status'] = 1;
        $data['paytype'] = 1;
        $data['money'] = $money;
        $data['month'] = date('Ym',time());
        $data['createtime'] = time();
        $data['pay_account']=$pay_account;
        $res2=M('user_trade')->add($data);
        if ($res1 && $res2){
            response(1,'','操作成功,金额稍后转入');
        }else{
            response(-1,'','系统错误');
        }
	}

	/**
	 * 充值接口
	 */
	public function recharge(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $uid=API_UID;
        $money = I('post.money', 0);
        $order_no=build_order_no();
        $data['uid']=$uid;
        $data['order_no'] =$order_no;
        $data['amount']=$money;
        $data['status'] =0;
        $data['create_time'] =time();
        $res=M('recharge_order')->add($data);
        //充值记录
        $map['uid']=$uid;
        $map['order_id']=$order_no;
        $map['type']   = 1;
        $map['money']  =$money;
        $map['month']  = date('Ym',time());;
        $map['status'] = 0;
        $map['createtime']=time();
        $re = M('user_trade')->add($map);
        $result['order_no']=$order_no;
        response(1,$result);
	}


    /**
     * 我的二维码
     */
    public function myqrcode(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $uid=API_UID;
        $driver = I('post.driver');
        $code= M('member')->where('uid='.$uid)->getField('qrcode');
        if($code){
            $data['qrcode'] =$code;
            response(1,$data);
        }
        $url = C('BASE_URI')."/api/Invitingfriends/index.html?uid=".$uid."&driver=".$driver;
        $filename=time().$uid;
        $qrcode  = qrcode($url,$filename);
        $qrcode=substr($qrcode,1);
        $data['qrcode']= C('BASE_URI').$qrcode;
        $res= M('member')->where('uid='.$uid)->save($data);

        $res? response(1,$data) : response(-9,'','邀请码生成失败');
    }

    /**
     * 推荐人列表
     */
    public  function   refereesList(){
        !api_login() && response(-94);
        !passed_login() && response(-95);
        !only_login() && response(-96);
        $uid = API_UID;
        $phone=M('ucenter_member')->where('id='.$uid)->getField('username');
        // $zlist = M('member')->where('referrer='.$phone)->select();
        $list = M('member m')
            ->join('jzb_ucenter_member u on m.uid=u.id')
            ->order('m.reg_time desc')
            ->where('referrer='.$phone)->select();
        foreach ($list as $k=>&$li){
            $username=M('ucenter_member')->where('id='.$li['uid'])->getField('username');
            $mobile=$username;
            $zr['mobile']=$mobile;
            $zr['nickname']=$li['nickname'];
            $zlist[] =$zr;
            $referrer = M('member')->where('referrer='.$username)->select();
            if($referrer) {
                foreach ($referrer as &$re){
                    $lre['mobile'] =M('ucenter_member')->where('id='.$re['uid'])->getField('username');
                    $lre['nickname']=$re['nickname'];
                    $jlist[] = $lre;
                }
            }
        }
        $data=array();
        if($zlist){
            $data['ztlist'] = $zlist;
            if($jlist){
                $jlist=array_merge($jlist);
                $data['jlist'] = $jlist;
            }
        }
        response(1,$data);
    }


}
?>
